//
//  SnapshotInfoDelegate.m
//  cTypica
//
//  Created by David Kavanagh on 11/20/08.
//  Copyright 2008 directThought. All rights reserved.
//

#import "SnapshotInfoDelegate.h"

@implementation SnapshotInfoDelegate
@synthesize snapshotInfo, formatter;

+ (id)delegate {
	SnapshotInfoDelegate *ret = [self alloc];
	ret.formatter = [[NSDateFormatter alloc] init];
	[ret.formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
	return ret;
}

- (void) dealloc {
	[formatter release];
	[super dealloc];
}

// XML parser delegate methods
- (void)parser: (NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	if ([elementName isEqualToString:@"snapshotSet"]) {
		self.snapshotInfo = [[NSMutableArray alloc] initWithCapacity:1];
	}
	else if ([elementName isEqualToString:@"item"]) {
		item = [SnapshotInfo alloc];

	}
}

- (void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	NSString *cleanString = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	[currentStringValue setString:@""];
	if ([elementName isEqualToString:@"snapshotSet"]) {
	}
	else if ([elementName isEqualToString:@"item"]) {
		[self.snapshotInfo addObject:item];
		[item release];
		item = nil;
	}
	else if ([elementName isEqualToString:@"snapshotId"]) {
		item.snapshotId = cleanString;
	}
	else if ([elementName isEqualToString:@"volumeId"]) {
		item.volumeId = cleanString;
	}
	else if ([elementName isEqualToString:@"status"]) {
		item.status = cleanString;
	}
	else if ([elementName isEqualToString:@"startTime"]) {
		item.startTime = [formatter dateFromString:cleanString];
		//[cleanString release];
	}
	else if ([elementName isEqualToString:@"progress"]) {
		item.progress = cleanString;
	}
}
@end
